(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	var setting = {
		check: {
			enable: true
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		view: {
			showIcon: false,
			showLine: false,
			addHoverDom: addHoverDom,
			removeHoverDom: removeHoverDom,
			selectedMulti: false
		},
		edit: {
			enable: true,
			editNameSelectAll: true,
			/*showRemoveBtn: showRemoveBtn,*/
			showRenameBtn: showRenameBtn
		},
		callback: {
			beforeDrag: beforeDrag,
			beforeEditName: beforeEditName,
			beforeRemove: beforeRemove,
			beforeRename: beforeRename,
			onRemove: onRemove,
			onRename: onRename
		}
	};
	var log, className = "dark";
	function beforeDrag(treeId, treeNodes) {
		return false;
	}
	function beforeEditName(treeId, treeNode) {
		className = (className === "dark" ? "":"dark");
		showLog("[ "+getTime()+" beforeEditName ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
		var zTree = $.fn.zTree.getZTreeObj("models-tree");
		zTree.selectNode(treeNode);	
		return confirm("确认编辑模块  " + treeNode.name + " 吗？");
	}
	function beforeRemove(treeId, treeNode) {
		className = (className === "dark" ? "":"dark");
		showLog("[ "+getTime()+" beforeRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
		var zTree = $.fn.zTree.getZTreeObj("models-tree");
		zTree.selectNode(treeNode);
		return confirm("确认删除模块 " + treeNode.name + " 吗？");
	}
	function onRemove(e, treeId, treeNode) {
		showLog("[ "+getTime()+" onRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
	}
	function beforeRename(treeId, treeNode, newName, isCancel) {
		className = (className === "dark" ? "":"dark");
		showLog((isCancel ? "<span style='color:red'>":"") + "[ "+getTime()+" beforeRename ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name + (isCancel ? "</span>":""));
		if (newName.length == 0) {
			alert("模块名称不能为空.");
			var zTree = $.fn.zTree.getZTreeObj("models-tree");
			setTimeout(function(){zTree.editName(treeNode);}, 10);
			return false;
		}
		return true;
	}
	function onRename(e, treeId, treeNode, isCancel) {
		showLog((isCancel ? "<span style='color:red'>":"") + "[ "+getTime()+" onRename ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name + (isCancel ? "</span>":""));
	}
//	function showRemoveBtn(treeId, treeNode) {
//		return !treeNode.isFirstNode;
//	}
	function showRenameBtn(treeId, treeNode) {
		return !(treeNode.level == 0);
	}
	function showLog(str) {
		if (!log) log = $("#log");
		log.append("<li class='"+className+"'>"+str+"</li>");
		if(log.children("li").length > 8) {
			log.get(0).removeChild(log.children("li")[0]);
		}
	}
	function getTime() {
		var now= new Date(),
		h=now.getHours(),
		m=now.getMinutes(),
		s=now.getSeconds(),
		ms=now.getMilliseconds();
		return (h+":"+m+":"+s+ " " +ms);
	}

	var newCount = 01;
	var idcount = 50;
	function addHoverDom(treeId, treeNode) {
		if(treeNode.level == 1 || treeNode.level == 2){
			var sObj = $("#" + treeNode.tId + "_span");
			if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
			var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
			+ "' title='add' onfocus='this.blur();'></span>";
			sObj.after(addStr);
			var btn = $("#addBtn_"+treeNode.tId);
			if (btn) btn.bind("click", function(){
				var count = newCount++;
				var zTree = $.fn.zTree.getZTreeObj("models-tree");
				var id = treeNode.id + (idcount++);
//				var children = treeNode.children;
//				var myid = "";
//				if(children != undefined){
//					myid = children[children.length-1].id + count;
//				}else{
//					myid = treeNode.id + count;
//				}
				var urlArr = treeNode.modelurl.split("/");
				var modelurl = "";
				$.each(urlArr,function(i){
					if(i < urlArr.length - 1){
						modelurl += urlArr[i] + "/";
					}
				});
				modelurl = modelurl + "newmodel" + count + "/newmodel" + count + ".html";
				var name = "new model" + count;
				zTree.addNodes(treeNode, {id:id, pId:treeNode.id, name:name, modelurl:modelurl});
				return false;
			});
			
		}else{
			return false;
		}
	};
	function removeHoverDom(treeId, treeNode) {
		$("#addBtn_"+treeNode.tId).unbind().remove();
	};
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initModelNodes();
		});
	});
	var modelNodes = [];
	function initModelNodes(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var data = {
				cmd:"queryModels",
				model:"license",
				userKey:userKey,
				data:{},
				remark:"",						
		};
		AjaxCrud.get2(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var root1 = {
						id: "selectAllModels",
						pId: -1,
						name : "全部",
						open: true,
						modelurl: "",
						icon: ""
				};
				modelNodes.push(root1);
				var data = res.data; 
				for(var i = 0; i < data.length; i++){
					var root2 = {
							id: data[i].code,
							pId: "selectAllModels",
							name : data[i].value,
							open: true,
							modelurl: data[i].url,
							icon: data[i].icon
					};
					modelNodes.push(root2);
					var children = data[i].children;
					for(var j = 0; j < children.length; j++){
						var root3 = {
								id: children[j].code,
								pId: data[i].code,
								name: children[j].value,
								open: true,
								modelurl: children[j].url,
								icon: children[j].icon
						};
						modelNodes.push(root3);
						var children1 = children[j].children;
						if(children1 != undefined){
							for(var k = 0; k < children1.length; k++){
								var root4 = {
										id: children1[k].code,
										pId: children[j].code,
										name: children1[k].value,
										open: true,
										modelurl: children1[k].url,
										icon: children1[k].icon,
								};
								modelNodes.push(root4);
							}
						}
					}
				}
				$.fn.zTree.init($("#models-tree"), setting, modelNodes);
			}else{
				
			}
		},null,null);
	}
	function editModelNodes(){
		
	}
	function showModelDialog(rolecode){
		var contents = "<div class='index-dialog-content-data'></div>";
		var obj = {
				width:400,
				height:300,
				title:"编辑",
				contents:contents
			  };
		userChoice(obj,function(){
			$("#sure-btn",parent.parent.document).unbind("click");
			$("#sure-btn",parent.parent.document).bind("click",function(){
				modelPower(rolecode);
				$(".index-dialog",parent.parent.document).hide();
				$(".index-dialog-cover",parent.parent.document).hide();
			});
			$("#cancel-btn",parent.parent.document).unbind("click");
			$("#cancel-btn",parent.parent.document).bind("click",function(){
				$(".index-dialog",parent.parent.document).hide();
				$(".index-dialog-cover",parent.parent.document).hide();
			});
		});
	}
	function saveModels(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var treeObj = $.fn.zTree.getZTreeObj("models-tree");
		var nodes = treeObj.getNodes()[0].children;
		var paramsArr = [];
		var params0 = null;
		var params1 = null;
		var params2 = null;
		if(nodes != undefined){
			for(var i = 0; i < nodes.length; i++){
				params0 = {};
				params0["code"] = nodes[i].id;
				params0["value"] = encodeURI(nodes[i].name);
				params0["url"] = nodes[i].modelurl;
				params0["icon"] = nodes[i].icon;
				paramsArr.push(params0);
				var nodes1 = nodes[i].children;
				if(nodes1 != undefined){
					for(var j = 0; j < nodes1.length; j++){
						params1 = {};
						params1["code"] = nodes1[j].id;
						params1["value"] = encodeURI(nodes1[j].name);
						params1["url"] = nodes1[j].modelurl;
						params1["icon"] = nodes1[j].icon;
						paramsArr.push(params1);
						var nodes2 = nodes1[j].children;
						if(nodes2 != undefined){
							for(var k = 0; k < nodes2.length; k++){
								params2 = {};
								paramsArr2 = [];
								params2["code"] = nodes2[k].id;
								params2["value"] = encodeURI(nodes2[k].name);
								params2["url"] = nodes2[k].modelurl;
								params2["icon"] = nodes2[k].icon;
								paramsArr.push(params2);
							}
						}
					}
				}
			}
		}else{
			var param = {};
			params["code"] = nodes[i].id;
			params["value"] = nodes[i].name;
			params["url"] = nodes[i].modelurl;
			paramsArr.push(param);
		}
		var data = {
				cmd:"updateModels",
				model:"license",
				userKey:userKey,
				data:JSON.stringify(paramsArr),
				remark:"",						
		};
		processBegin("正在运行中···");
		AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("模块保存成功");
			}else{
				processError("模块保存失败");
			}
		},null,null);
	}
	
	function substr(str,len){
		if(str != undefined){
			if(str.length > len){
				str = str.substring(0,len-2)+"...";
				return str;
			}else{
				return str;
			}
		}else{
			return "";
		}
	}
	function isUndefined(param){
		if(param == undefined){
			param = "";
		}
		return param;
	}
	function pagebtns(){
		$("#save-btns").click(function(){
			saveModels();
		});
	}

	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();